package com.csoa.community.utils;

import android.app.Activity;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.widget.Toast;

public class OverrideGestureListener extends SimpleOnGestureListener {
	private float distance = 100;
	private float velocity = 1000;
	private Activity activity;
	public OnPageProcessListener onPageProcessListener;

	public OverrideGestureListener(Activity a,OnPageProcessListener onPageProcessListener) {
		activity = a;
		this.onPageProcessListener = onPageProcessListener;
	}

	@Override
	public boolean onDown(MotionEvent e) {
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if (e1 == null || e2 == null) {
			return false;
		}
		float x1 = e1.getX();
		float x2 = e2.getX();
		float y1 = e1.getY();
		float y2 = e2.getY();
		float X = Math.abs(x2 - x1);
		float Y = Math.abs(y2 - y1);
		if (Y > X) {
			return false;
		}
		if (Math.abs(velocityX) > velocity) {
			if (x2 - x1 > distance) {
				// 上一页
//				Toast.makeText(activity, "上一页", Toast.LENGTH_LONG).show();
				onPageProcessListener.onPrePage();
				return true;
			} else if (x2 - x1 < -distance) {
				// 下一页
				onPageProcessListener.onNextPage();
//				Toast.makeText(activity, "下一页", Toast.LENGTH_LONG).show();
			}
		}

		return super.onFling(e1, e2, velocityX, velocityY);
	}

	@Override
	public void onLongPress(MotionEvent e) {

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}
	
	public interface OnPageProcessListener {
		public void onPrePage();
		public void onNextPage();
	}
	
	
}

